using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Nuclex.UserInterface;
using Nuclex.Input;

namespace BrackIT
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Bracket bracket;

        private GuiManager gui;
        private InputManager input;

        private Nuclex.UserInterface.Controls.LabelControl participants;
        private List<Nuclex.UserInterface.Controls.Desktop.InputControl> participantTextFields;
        private List<Nuclex.UserInterface.Controls.LabelControl> participantLabels;
        private List<String> participantNames;
        private Nuclex.UserInterface.Controls.Desktop.ButtonControl getParticipantsButton;

        private int teamSize;
        private int participantsPerRound;

        private Screen mainScreen;

        private int NUMBER_OF_PARTICIPANTS = 4;

        //Texts for the program
        private const String ENTER_PARTICIPANTS_NAMES = "Enter the names of the participants:";
        private const String UPDATE_PARTICIPANTS = "Update participants!";

        public Game1()
        {
            this.graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.input = new InputManager(Services, Window.Handle);
            this.gui = new GuiManager(Services);

            Components.Add(this.input);
            Components.Add(this.gui);

            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Viewport viewport = GraphicsDevice.Viewport;
            mainScreen = new Screen(viewport.Width, viewport.Height);
            this.gui.Screen = mainScreen;

            InitializeComponent(mainScreen);

            base.Initialize();
        }

        private void InitializeComponent(Screen mainScreen)
        {
            participants = new Nuclex.UserInterface.Controls.LabelControl();

            participantTextFields = new List<Nuclex.UserInterface.Controls.Desktop.InputControl>();
            for (int i = 0; i < NUMBER_OF_PARTICIPANTS; i++)
            {
                participantTextFields.Add(new Nuclex.UserInterface.Controls.Desktop.InputControl());
            }

            participants.Text = ENTER_PARTICIPANTS_NAMES;
            participants.Bounds = new UniRectangle(75.0f, 15.0f, 110.0f, 30.0f);

            participantNames = new List<String>();

            mainScreen.Desktop.Children.Add(participants);
            for (int i = 0; i < participantTextFields.Count; i++ )
            {
                participantTextFields[i].Bounds = new UniRectangle(75.0f, 40.0f*(i+1), 150.0f, 30.0f);
                participantTextFields[i].Enabled = true;
                mainScreen.Desktop.Children.Add(participantTextFields[i]);
            }

            getParticipantsButton = new Nuclex.UserInterface.Controls.Desktop.ButtonControl();
            getParticipantsButton.Bounds = new UniRectangle(mainScreen.Width / 2 - 80, mainScreen.Height / 2 - 15, 160, 30);
            getParticipantsButton.Text = UPDATE_PARTICIPANTS;
            getParticipantsButton.Enabled = true;

            participantLabels = new List<Nuclex.UserInterface.Controls.LabelControl>();

            getParticipantsButton.Pressed += delegate(object sender, EventArgs arguments)
            {
                participantNames.Clear();

                foreach(Nuclex.UserInterface.Controls.Desktop.InputControl iC in participantTextFields)
                {
                    if(!iC.Text.Equals("") && iC.Text != null)
                        participantNames.Add(iC.Text);
                }

                if (bracket == null)
                {
                    bracket = new Bracket(this, mainScreen, participantNames);
                    bracket.Initialize();
                }
                updateParticipants(participantNames);
            };

            mainScreen.Desktop.Children.Add(getParticipantsButton);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        private void updateParticipants(List<String> ps)
        {
            bracket.updateParticipants(ps);
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            if(bracket != null)
                bracket.Update(gameTime);
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Azure);

            // TODO: Add your drawing code here
            if (bracket != null)
                bracket.Draw(gameTime);
            
            base.Draw(gameTime);
        }
    }
}
